package com.satyam.smooth.beanmodeltable;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import com.satyam.smooth.domain.Item;
import com.satyam.smooth.domain.ItemGroup;

public class ItemTableModelTest extends RowTableModel<Item>{
	
	private List<Item> items = getItems();
	private static String[] COLUMN_NAMES = { "Name", "Code", "Item Group", "Action" };
	
	public static List<Item> getItems(){
		List<Item> itemList = new ArrayList<Item>();
	     itemList.add(new Item("Item 1", "Itm 1", new ItemGroup("Item Group 1", "IG 1")));
	     itemList.add(new Item("Item 2", "Itm 2", new ItemGroup("Item Group 2", "IG 2")));
	     itemList.add(new Item("Item 3", "Itm 3", new ItemGroup("Item Group 1", "IG 1")));
	     return itemList;
	}
	
	List<ItemGroup> getItemGroup(){
		List<ItemGroup> itemGroupList = new ArrayList();
	     itemGroupList.add(new ItemGroup("Item Group 1", "IG 1"));
	     itemGroupList.add(new ItemGroup("Item Group 2", "IG 2"));
	     itemGroupList.add(new ItemGroup("Item Group 3", "IG 3"));
	     itemGroupList.add(new ItemGroup("Item Group 3", "IG 4"));
	     return itemGroupList;
	}
	
	 
      
    

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		Item item = items.get(rowIndex);
		switch (columnIndex) {
		case 0:
			return item.getName();
		case 1:
			return item.getCode();
		case 2:
			return item.getItemGroup();
		default:
			return "";
		}

	}
	
	@Override
	public void setValueAt(Object value, int rowIndex, int columnIndex) {
		Item item = items.get(rowIndex);
		
		switch (columnIndex) {
		case 0:
			item.setName((String) value);
			break;
		case 1:
			item.setCode((String) value);
			break;
		case 2:
			item.setItemGroup((ItemGroup) value);
			break;
		}
		
		fireTableCellUpdated(rowIndex, columnIndex);
		
	}

	public ItemTableModelTest() {
		super( Arrays.asList(COLUMN_NAMES) );
		setRowClass( Item.class );

		setColumnClass(2, ItemGroup.class);
		
		
	}
	
	public static void main(String[] args)
	{
		 
		ItemTableModelTest model = new ItemTableModelTest();
		for (Item item : getItems()) {
			model.addRow(item);
		}
		
		JTable table = new JTable(model);
		table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
		JScrollPane scrollPane = new JScrollPane( table );

	 
		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		frame.getContentPane().add( scrollPane );
		frame.pack();
		frame.setLocationRelativeTo( null );
		frame.setVisible(true);

		 
	}
	
	 
}
